package com.JUC.cha03.sec06;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

/**
 * 1.作业
 *  合并结果集thenCombine
 */
public class hw01_CompletableFutureDemo {

    public static void main(String[] args) {

        CompletableFuture<Integer> result = CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in 1");
            // 暂停几秒钟线程
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 10;
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in 2");
            // 暂停几秒钟线程
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 20;
        }), (k1, k2) -> {
            System.out.println(Thread.currentThread().getName() + "\t come in combine1");
            System.out.println("合并 k1 、 k2");
            return k1 + k2;
        }).thenCombine(CompletableFuture.supplyAsync(() -> {
            System.out.println(Thread.currentThread().getName() + "\t come in 3");
            // 暂停几秒钟线程
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 30;
        }), (k3, k4) -> {
            System.out.println(Thread.currentThread().getName() + "\t come in combine2");
            System.out.println("合并 k3 、 k4");
            return k3 + k4;
        });

        System.out.println(Thread.currentThread().getName() + "结束");
        System.out.println(result.join());

    }
}
